package com.agileai.em.service.expenses;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.agileai.common.KeyGenerator;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.em.common.ProcessHelper;
import com.agileai.em.cxmodule.BizCodeUtil;
import com.agileai.em.cxmodule.EmEvectionManage;
import com.agileai.em.cxmodule.EmExpReimbursementManage;
import com.agileai.em.wsproxy.ActivityItem;
import com.agileai.em.wsproxy.BizAttribute;
import com.agileai.em.wsproxy.DraftWorkItem;
import com.agileai.em.wsproxy.LaunchWorkItem;
import com.agileai.em.wsproxy.ProcessProfile;
import com.agileai.em.wsproxy.ProcessService;
import com.agileai.em.wsproxy.WorkItem;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.ws.BaseRestService;
import com.agileai.util.DateUtil;
import com.agileai.util.StringUtil;

public class EvectionReiDataProviderImpl extends BaseRestService implements EvectionReiDataProvider{
    
	private static String FAIL = "fail";
	private static String SUCCESS = "success";
	private static String AUDITING = "AUDITING";
	private static String UNSUBMITTED = "UNSUBMITTED";
	public static final String PROCESS_CODE = "Emevection";
	
	@Override
	public String retrieveEvectionReiList() {
		String responseText = FAIL;
		DataParam param = new DataParam();
		try {
			EmEvectionManage emEvectionManage = this.lookupService(EmEvectionManage.class);
			User user = (User) this.getUser();
			param.put("EVE_APPLICATION",user.getUserId());
			List<DataRow> records = emEvectionManage.mobileFindEvectionRecords(param);
			JSONArray jsonArray = new JSONArray();
			for(int i=0;i<records.size();i++){
				JSONObject jsonObject = new JSONObject();
				DataRow row = records.get(i);
				String ID = row.getString("EVE_ID");
				String TITLE = row.getString("PC_NAME");
				String NAME = row.getString("EVE_APPLICATION_NAME");
				Date erDate = (Date) row.get("EVE_REIMBURSEMENT_TIME");
				String DATE = DateUtil.getDateByType(DateUtil.YYMMDDHHMI_SLANTING, erDate);
				String STATE = row.getString("EVE_STATE");
				String STATE_TEXT = row.getString("EVE_STATE_TEXT");
				String money = row.stringValue("EVE_TOTAL_MONEY");
				
				ProcessHelper processHelper = ProcessHelper.instance();
			    ProcessService bpmService = processHelper.getBPMService();
			    WorkItem workItem = this.getActivityName(bpmService, ID);
			    
			    String activityName = "";
			    if(workItem != null){
					/*String laucherName = workItem.getLaucherName();
					String laucheTime = workItem.getLaucheTime();*/
					activityName = workItem.getActivityName();
				}
				
			    jsonObject.put("id", ID);
			    jsonObject.put("theme", TITLE);
			    jsonObject.put("submitterName", NAME);
			    jsonObject.put("commitTime", DATE);
			    jsonObject.put("state", STATE);
			    jsonObject.put("checkStateName", STATE_TEXT);
			    jsonObject.put("money", money);
			    jsonObject.put("type", "差旅报销");
			    jsonObject.put("commitType", STATE_TEXT);
			    jsonObject.put("checkNodeName", activityName);
				  
				jsonArray.put(jsonObject);
			}
			responseText = jsonArray.toString();
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		
		return responseText;
	}
	

	@Override
	public String retrieveEvectionReiDetail(String id) {
		String result = null;
		DataParam param = new DataParam();
		try {
			EmEvectionManage emEvectionManage = this.lookupService(EmEvectionManage.class);
			List<DataRow> approveList = emEvectionManage.findAppList(id);
			DataRow record = emEvectionManage.getTaskEveRecord(id);
			JSONObject jsonObject = new JSONObject();
			if(record!=null){
				String eveId = record.getString("EVE_ID");
				String title = record.getString("PC_NAME");
				String name = record.getString("EVE_APPLICATION_NAME");
				String together = record.getString("EVE_TOGETHER");
				if(StringUtil.isNullOrEmpty(together)){
					together = "无";
				}
				Date erDate = (Date) record.get("EVE_REIMBURSEMENT_TIME");
				String date = DateUtil.getDateByType(DateUtil.YYMMDD_SLANTING, erDate);
				Date sDate = (Date) record.get("EVE_START_TIME");
				String startDate = DateUtil.getDateByType(DateUtil.YYMMDD_SLANTING, sDate);
				Date eDate = (Date) record.get("EVE_OVER_TIME");
				String overDate = DateUtil.getDateByType(DateUtil.YYMMDD_SLANTING, eDate);
				int days = record.getInt("EVE_DAYS");
				BigDecimal subsidy = (BigDecimal) record.get("EVE_SUBSIDY");
				BigDecimal money = (BigDecimal) record.get("EVE_TOTAL_MONEY");
				String remark = record.getString("EVE_REASON");
				String STATE = record.getString("EVE_STATE");
				
				if(StringUtil.isNullOrEmpty(remark)){
					remark = "无";
				}
				String stateText = record.getString("EVE_STATE_TEXT");
				jsonObject.put("id", eveId);
				jsonObject.put("title", title);
				jsonObject.put("name", name);
				jsonObject.put("togetherPerson", together);
				jsonObject.put("commitTime", date);
				jsonObject.put("startTime", startDate);
				jsonObject.put("endTime", overDate);
				jsonObject.put("evectionDay", days);
				jsonObject.put("subsidy", subsidy);
				jsonObject.put("money", money);
				jsonObject.put("desc", remark);
				jsonObject.put("stateText", stateText);
				jsonObject.put("commitType", STATE);
				//清单
				param.put("EVE_ID",id);
				List<DataRow> records = emEvectionManage.findExpRecords(param);
				JSONArray jsonArray = new JSONArray();
				for(int i=0;i<records.size();i++){
					JSONObject jsonObject1 = new JSONObject();
					DataRow row = records.get(i);
					String ID = row.getString("EXP_ID");
					String departure = row.getString("EXP_DEPARTURE");
					String destination = row.getString("EXP_DESTINATION");
					String sdate = DateUtil.getDateByType(DateUtil.YYMMDDHHMI_SLANTING,(Date) row.get("EXP_START_TIME"));
					String edate = DateUtil.getDateByType(DateUtil.YYMMDDHHMI_SLANTING, (Date) row.get("EXP_END_TIME"));
					String transportation_way = row.getString("EXP_TRANSPORTATION_WAY_TEXT");
					String transportation = row.getString("EXP_TRANSPORTATION_WAY");
					BigDecimal transportation_fee = (BigDecimal)row.get("EXP_TRANSPORTATION_FEE");
					BigDecimal horel_fee = (BigDecimal)row.get("EXP_HOREL_FEE");
					BigDecimal other_fee = (BigDecimal) row.get("EXP_OTHER_FEE");
					String remarks = row.getString("EXP_REMARKS");
					if(StringUtil.isNullOrEmpty(remarks)){
						remarks = "无";
					}
					
					JSONArray selectJsonArr = this.getSelectList("TRANSPORTATION_WAY",transportation);
					jsonObject1.put("traSelect", selectJsonArr);
					String state = row.getString("STATE");
					jsonObject1.put("expId", ID);
					jsonObject1.put("departure", departure);
					jsonObject1.put("destination", destination);
					jsonObject1.put("sdate", sdate);
					jsonObject1.put("edate", edate);
					jsonObject1.put("transportationWay", transportation_way);
					jsonObject1.put("transportation", transportation);
					jsonObject1.put("transportationFee", transportation_fee);
					jsonObject1.put("horelFee", horel_fee);
					jsonObject1.put("otherFee", other_fee);
					jsonObject1.put("remarks", remarks);
					jsonObject1.put("state", state);
					jsonArray.put(jsonObject1);
				}
				jsonObject.put("expenseList", jsonArray);
			}
			JSONArray jsonArray = new JSONArray();
			for (int i = 0; i < approveList.size(); i++) {
				DataRow approveRow = approveList.get(i);
				JSONObject jsonObject1 = new JSONObject();
				jsonObject1.put("id", approveRow.getString("APP_ID"));
				jsonObject1.put("approver", approveRow.getString("APPROVER"));
				jsonObject1.put("appDateTime", approveRow.get("APP_DATETIME"));
				jsonObject1.put("appResult", approveRow.getString("APP_RESULT"));
				jsonObject1.put("appResultCode", approveRow.getString("APP_RESULT_CODE"));
				String appOpinion = approveRow.getString("APP_OPINION");
				if(StringUtil.isNullOrEmpty(appOpinion)){
					appOpinion = "无";
				}
				jsonObject1.put("appOpinion", appOpinion);
				jsonArray.put(jsonObject1);
			}
			jsonObject.put("appList", jsonArray);
			result = jsonObject.toString();
		}catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return result;
		
	}
	
	@Override
	public String getProjectSelect() {
		EmEvectionManage emEvectionManage = this.lookupService(EmEvectionManage.class);
		List<DataRow> list = emEvectionManage.getProjectSelectList();
		DataRow emptyItemRow = new DataRow();
		emptyItemRow.put("code", "");
		emptyItemRow.put("name", "请选择");
		list.add(0, emptyItemRow);
		JSONArray jsonArr = new JSONArray();
		try {
			for(int i = 0;i < list.size(); i++){
				DataRow row = list.get(i);
				JSONObject jsonObj = new JSONObject();
				jsonObj.put("code", row.stringValue("code"));
				jsonObj.put("name", row.stringValue("name"));
				if(StringUtil.isNullOrEmpty(row.stringValue("code"))){
					jsonObj.put("selected", true);
				}else{
					jsonObj.put("selected", false);
				}
				jsonArr.put(jsonObj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jsonArr.toString();
	}
	

	@Override
	public String saveEvectionDetailInfo(DataParam detailInfoParam) {
		String responseText = FAIL;
		EmEvectionManage emEvectionManage = this.lookupService(EmEvectionManage.class);
		String operationType = detailInfoParam.stringValue("operationType");
		User user = (User) this.getUser();
		
		String reiListStr = detailInfoParam.stringValue("expenseList");
		
		String key = KeyGenerator.instance().genKey();
		
		String EVE_ID = detailInfoParam.stringValue("id");
		if(StringUtil.isNullOrEmpty(EVE_ID)){
			EVE_ID = key;
		}
		
		String PC_ID = detailInfoParam.stringValue("projectId");
		String EVE_APPLICATION = user.getUserId();
		String EVE_TOGETHER = detailInfoParam.stringValue("togetherPerson");
		String EVE_START_TIME =this.dateFormat( detailInfoParam.stringValue("startTime"));
		String EVE_OVER_TIME = this.dateFormat( detailInfoParam.stringValue("endTime"));
		String EVE_DAYS = detailInfoParam.stringValue("evectionDay");
		String EVE_REASON = detailInfoParam.stringValue("desc");
		String EVE_SUBSIDY = detailInfoParam.stringValue("subsidy");
		String EVE_TOTAL_MONEY = detailInfoParam.stringValue("money");
		String EVE_STATE = detailInfoParam.stringValue("commitType");
		
		DataParam param = new DataParam();
		param.put("EVE_ID", EVE_ID);
		param.put("PC_ID", PC_ID);
		param.put("EVE_APPLICATION", EVE_APPLICATION);
		param.put("EVE_TOGETHER", EVE_TOGETHER);
		param.put("EVE_START_TIME", EVE_START_TIME);
		param.put("EVE_OVER_TIME", EVE_OVER_TIME);
		param.put("EVE_REIMBURSEMENT_TIME", new Date());
		param.put("EVE_DAYS", EVE_DAYS);
		param.put("EVE_REASON", EVE_REASON);
		param.put("EVE_SUBSIDY", EVE_SUBSIDY);
		param.put("EVE_TOTAL_MONEY", EVE_TOTAL_MONEY);
		if(param.get("EVE_TOTAL_MONEY").isEmpty()){
			param.put("EVE_TOTAL_MONEY","0.00");
		}
		
		param.put("EVE_STATE", EVE_STATE);
		
		param.put("sdate",DateUtil.getDateByType(DateUtil.YYMMDD_SLANTING, new Date())+" 00:00:00");
		param.put("edate",DateUtil.getDateByType(DateUtil.YYMMDD_SLANTING, new Date())+" 23:59:59");
		
		List<DataRow> list = emEvectionManage.findMasterRecords(param);
		String bizCode = BizCodeUtil.create("EVE", list, BizCodeUtil.withPrefix);
		param.put("EVE_CODE",bizCode);
		if("insert".equals(operationType)){
			emEvectionManage.createMasterRecord(param);
			String PC_NAME = emEvectionManage.getProjectName(PC_ID);
			String EVE_APPLICATION_NAME = emEvectionManage.getUserName(user.getUserId());
			
			ProcessHelper processHelper = ProcessHelper.instance();
	   		ProcessService processService = processHelper.getBPMService();
	   		String title = PC_NAME+ "_" + EVE_APPLICATION_NAME;
	   		String uuId = param.get("EVE_ID");
	   		DraftWorkItem draftWorkItem = new DraftWorkItem();
	   		draftWorkItem.setProcessCode(PROCESS_CODE);
	   		draftWorkItem.setUserCode(user.getUserCode());
	   		draftWorkItem.setBizTitle(title);
	   		draftWorkItem.setBizRecordId(uuId);
	   		draftWorkItem.setBizRecordCode(param.getString("EVE_CODE"));
	   		processService.saveDraft(draftWorkItem);
		}else if("edit".equals(operationType)){
			emEvectionManage.updateMasterRecord(param);
		}
   		//保存清单
		try {
			JSONArray reiListArr = new JSONArray(reiListStr);
			for(int i = 0; i < reiListArr.length(); i++){
				JSONObject jsonObj = reiListArr.getJSONObject(i);
				String EXP_ID = (String) jsonObj.get("expId");
				//String EVE_ID = (String) jsonObj.get("");
				String EXP_DEPARTURE = (String) jsonObj.get("departure");
				String EXP_DESTINATION = (String) jsonObj.get("destination");
				String EXP_START_TIME =this.dateFormat( (String) jsonObj.get("sdate"));
				String EXP_END_TIME =this.dateFormat( (String) jsonObj.get("edate"));
				String EXP_TRANSPORTATION_WAY = (String) jsonObj.get("transportation");
				String EXP_TRANSPORTATION_FEE =  jsonObj.get("transportationFee").toString();
				String EXP_HOREL_FEE =  jsonObj.get("horelFee").toString();
				String EXP_OTHER_FEE =  jsonObj.get("otherFee").toString();
				String EXP_REMARKS = (String) jsonObj.get("remarks");
				 
				DataParam reiListParam = new DataParam();
				
				reiListParam.put("EVE_ID", EVE_ID);
				reiListParam.put("EXP_DEPARTURE", EXP_DEPARTURE);
				reiListParam.put("EXP_DESTINATION", EXP_DESTINATION);
				reiListParam.put("EXP_START_TIME", EXP_START_TIME);
				reiListParam.put("EXP_END_TIME", EXP_END_TIME);
				reiListParam.put("EXP_TRANSPORTATION_WAY", EXP_TRANSPORTATION_WAY);
				reiListParam.put("EXP_TRANSPORTATION_FEE", EXP_TRANSPORTATION_FEE);
				reiListParam.put("EXP_HOREL_FEE", EXP_HOREL_FEE);
				reiListParam.put("EXP_OTHER_FEE", EXP_OTHER_FEE);
				reiListParam.put("EXP_REMARKS", EXP_REMARKS);
				
				if(reiListParam.get("EXP_TRANSPORTATION_FEE").isEmpty()){
					reiListParam.put("EXP_TRANSPORTATION_FEE","0.00");
				}
				if(reiListParam.get("EXP_HOREL_FEE").isEmpty()){
					reiListParam.put("EXP_HOREL_FEE","0.00");
				}
				if(reiListParam.get("EXP_OTHER_FEE").isEmpty()){
					reiListParam.put("EXP_OTHER_FEE","0.00");
				}
				
				if(StringUtil.isNullOrEmpty(EXP_ID)){
					EXP_ID = KeyGenerator.instance().genKey();
					reiListParam.put("EXP_ID", EXP_ID);
					emEvectionManage.createSubRecord("EmExpenses", reiListParam);
				}else{
					reiListParam.put("EXP_ID", EXP_ID);
					emEvectionManage.updateSubRecord("EmExpenses", reiListParam);
				}
				
			}
			
			String masterRecordId = param.get("EVE_ID");
			emEvectionManage.computeTotalMoney(masterRecordId);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		responseText = SUCCESS;	
		return responseText;
	}
	
	@Override
	public String submitEvectionDetailInfo(String id) {
		String responseText = FAIL;
		EmEvectionManage emEvectionManage = this.lookupService(EmEvectionManage.class);
		User user = (User) this.getUser();
		DataParam param = new DataParam();
		param.put("EVE_ID", id);
		DataRow record = emEvectionManage.getMasterRecord(param);
		
		try {
			List<DataRow> subRecords = emEvectionManage.findSubRecords("EmExpenses", param);
			if(subRecords.size()<1){
				responseText = "nosub";
			}else{
				param.put("EVE_STATE", "AUDITING");
				emEvectionManage.updateMasterRecordState(param);
				ProcessHelper processHelper = ProcessHelper.instance();
				ProcessService processService = processHelper.getBPMService();
				List<BizAttribute> attributeList = new ArrayList<BizAttribute>();
				BizAttribute bizAttribute = new BizAttribute();
				bizAttribute.setCode("userId");
				bizAttribute.setValue(user.getUserId());
				attributeList.add(bizAttribute);
				String bizRecordId = param.get("EVE_ID");
				
				String bizRecordCode = record.stringValue("EVE_CODE");
				if(StringUtil.isNullOrEmpty(bizRecordId)){
					bizRecordId = KeyGenerator.instance().genKey();
				}
				String processId = processService.getCurrentProcessId(PROCESS_CODE);
				
				String title = record.stringValue("PC_NAME")+"_"+record.stringValue("EVE_APPLICATION_NAME");
				LaunchWorkItem launchWorkItem = processHelper.createLaunchWorkItem(processId);
				launchWorkItem.getAttributeList().addAll(attributeList);
				launchWorkItem.setTitle(title);
				launchWorkItem.setBizRecordId(bizRecordId);
				launchWorkItem.setBizRecordCode(bizRecordCode);
				launchWorkItem.setUserCode(user.getUserCode());		
				param.put("SKIP_FIRST_NODE", "true");
				boolean skipFirstNode = "true".equals(param.get("SKIP_FIRST_NODE"));
				processService.launchProcess(launchWorkItem,skipFirstNode);
				responseText = SUCCESS;
			}
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return responseText;
	}
	
	@Override
	public String retrieveEvectionReiDetailTast(String id) {
		String result = null;
		DataParam param = new DataParam();
		try {
			EmEvectionManage emEvectionManage = this.lookupService(EmEvectionManage.class);
			List<DataRow> approveList = emEvectionManage.findAppList(id);
			DataRow record = emEvectionManage.getTaskEveRecord(id);
			JSONObject jsonObject = new JSONObject();
			if(record!=null){
				String eveId = record.getString("EVE_ID");
				String title = record.getString("PC_NAME");
				String name = record.getString("EVE_APPLICATION_NAME");
				String together = record.getString("EVE_TOGETHER");
				if(StringUtil.isNullOrEmpty(together)){
					together = "无";
				}
				Date erDate = (Date) record.get("EVE_REIMBURSEMENT_TIME");
				String date = DateUtil.getDateByType(DateUtil.YYMMDD_SLANTING, erDate);
				Date sDate = (Date) record.get("EVE_START_TIME");
				String startDate = DateUtil.getDateByType(DateUtil.YYMMDD_SLANTING, sDate);
				Date eDate = (Date) record.get("EVE_OVER_TIME");
				String overDate = DateUtil.getDateByType(DateUtil.YYMMDD_SLANTING, eDate);
				int days = record.getInt("EVE_DAYS");
				BigDecimal subsidy = (BigDecimal) record.get("EVE_SUBSIDY");
				BigDecimal money = (BigDecimal) record.get("EVE_TOTAL_MONEY");
				String remark = record.getString("EVE_REASON");
				String STATE = record.getString("EVE_STATE");
				
				if(StringUtil.isNullOrEmpty(remark)){
					remark = "无";
				}
				String stateText = record.getString("EVE_STATE_TEXT");
				jsonObject.put("id", eveId);
				jsonObject.put("title", title);
				jsonObject.put("name", name);
				jsonObject.put("togetherPerson", together);
				jsonObject.put("commitTime", date);
				jsonObject.put("startTime", startDate);
				jsonObject.put("endTime", overDate);
				jsonObject.put("evectionDay", days);
				jsonObject.put("subsidy", subsidy);
				jsonObject.put("money", money);
				jsonObject.put("desc", remark);
				jsonObject.put("stateText", stateText);
				jsonObject.put("commitType", STATE);
				//清单
				param.put("EVE_ID",id);
				List<DataRow> records = emEvectionManage.findExpRecords(param);
				JSONArray jsonArray = new JSONArray();
				for(int i=0;i<records.size();i++){
					JSONObject jsonObject1 = new JSONObject();
					DataRow row = records.get(i);
					String ID = row.getString("EXP_ID");
					String departure = row.getString("EXP_DEPARTURE");
					String destination = row.getString("EXP_DESTINATION");
					String sdate = DateUtil.getDateByType(DateUtil.YYMMDDHHMI_SLANTING,(Date) row.get("EXP_START_TIME"));
					String edate = DateUtil.getDateByType(DateUtil.YYMMDDHHMI_SLANTING, (Date) row.get("EXP_END_TIME"));
					String transportation_way = row.getString("EXP_TRANSPORTATION_WAY_TEXT");
					String transportation = row.getString("EXP_TRANSPORTATION_WAY");
					BigDecimal transportation_fee = (BigDecimal)row.get("EXP_TRANSPORTATION_FEE");
					BigDecimal horel_fee = (BigDecimal)row.get("EXP_HOREL_FEE");
					BigDecimal other_fee = (BigDecimal) row.get("EXP_OTHER_FEE");
					String remarks = row.getString("EXP_REMARKS");
					if(StringUtil.isNullOrEmpty(remarks)){
						remarks = "无";
					}
					
					JSONArray selectJsonArr = this.getSelectList("TRANSPORTATION_WAY",transportation);
					jsonObject1.put("traSelect", selectJsonArr);
					String state = row.getString("STATE");
					jsonObject1.put("expId", ID);
					jsonObject1.put("departure", departure);
					jsonObject1.put("destination", destination);
					jsonObject1.put("sdate", sdate);
					jsonObject1.put("edate", edate);
					jsonObject1.put("transportationWay", transportation_way);
					jsonObject1.put("transportation", transportation);
					jsonObject1.put("transportationFee", transportation_fee);
					jsonObject1.put("horelFee", horel_fee);
					jsonObject1.put("otherFee", other_fee);
					jsonObject1.put("remarks", remarks);
					jsonObject1.put("state", state);
					jsonArray.put(jsonObject1);
				}
				jsonObject.put("expenseList", jsonArray);
			}
			JSONArray jsonArray = this.getAppListArr("Emevection", id);
			jsonObject.put("appList", jsonArray);
			result = jsonObject.toString();
		}catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return result;
		
	}
	
	private WorkItem getActivityName(ProcessService bpmService,String id){
		WorkItem workItem = null;
		List<WorkItem> taskItemList = bpmService.findWorkItems("");
		for (int i = 0; i < taskItemList.size(); i++) {
			WorkItem taskItem = taskItemList.get(i);
			String bizId = taskItem.getBusinessId();
			
			if(bizId.equals(id)){
				workItem =  taskItem;
			}
		}
		return workItem;
	}
	
	private JSONArray getSelectList(String TypeId,String selectedId) throws JSONException{
		EmExpReimbursementManage emExpReimbursementManage = this.lookupService(EmExpReimbursementManage.class);
		List<DataRow> list = emExpReimbursementManage.getSelectList(TypeId);
		
		DataRow emptyItemRow = new DataRow();
		emptyItemRow.put("code", "");
		emptyItemRow.put("name", "请选择");
		list.add(0, emptyItemRow);
		
		JSONArray jsonArr = new JSONArray();
		for(int i = 0;i < list.size(); i++){
			DataRow row = list.get(i);
			JSONObject jsonObj = new JSONObject();
			jsonObj.put("code", row.stringValue("code"));
			jsonObj.put("name", row.stringValue("name"));
			if(selectedId.equals(row.stringValue("code"))){
				jsonObj.put("selected", true);
			}else{
				jsonObj.put("selected", false);
			}
			jsonArr.put(jsonObj);
		}
		return jsonArr;
	}
    
	private String dateFormat(String dateStr){
		String result = "";
		if(StringUtil.isNotNullNotEmpty(dateStr)){
			try {
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				Date date = formatter.parse(dateStr);
				result = DateUtil.getDateByType(DateUtil.YYMMDDHHMI_SLANTING,date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	private JSONArray getAppListArr(String processCode,String bussinessId ) throws JSONException{
		JSONArray arr = new JSONArray();
		DataParam param = new DataParam();
		param.put("EVE_ID",bussinessId);
		ProcessHelper processHelper = ProcessHelper.instance();
		ProcessService bpmService = processHelper.getBPMService();
		EmEvectionManage emEvectionManage = this.lookupService(EmEvectionManage.class);
		
		ProcessProfile processProfile = bpmService.loadProcessProfile(processCode, bussinessId);
		HashMap<String, DataRow> records = emEvectionManage.findApproveMobilityRecords(param);
		List<ActivityItem> processList = processProfile.getActivityItems();
	    Collections.sort(processList, new Comparator<ActivityItem>() {
			@Override
			public int compare(ActivityItem a, ActivityItem b) {
				String timeA = a.getOperatorTime();
				String timeB = b.getOperatorTime();
				if(StringUtil.isNullOrEmpty(timeA)||StringUtil.isNullOrEmpty(timeB)){
					return 0;
				}
				return timeA.compareTo(timeB);
			}  
		 });
		
	    int processListSize = processList.size();
	    Collections.reverse(processList);
		
		for(int i=0;i<processListSize;i++){
			ActivityItem activityItem = processList.get(i);
			DataRow row = new DataRow();
			String activityInstId = activityItem.getActivityInstId();
			String activityInstType = activityItem.getItemType();
			String appName = activityItem.getOperatorName();
			String appDate = activityItem.getOperatorTime();
			String actName = activityItem.getActivityName();
			String appResult = activityItem.getActivityName();
			String imgUrl = "pass.png";
			String imgBg = "#75ce66";
			if("instance".equals(activityInstType)){
				imgUrl = "wait.png";
				imgBg = "#ffdf00";
			}
			
			if(StringUtil.isNullOrEmpty(appName)){
				appName =actName;
			}
			
			String appOpinion = "";
			if(records.containsKey(activityInstId)){
				row = records.get(activityInstId);
				appResult = row.getString("EVE_APP_RESULT_NAME");
				appOpinion = row.getString("EVE_APP_OPINION");
			}
			
			JSONObject jsonObject1 = new JSONObject();
			jsonObject1.put("appName", appName);
			jsonObject1.put("appResult", appResult);
			jsonObject1.put("appDate", appDate);
			jsonObject1.put("appOpinion",appOpinion);
			jsonObject1.put("imgUrl",imgUrl);
			jsonObject1.put("imgBg",imgBg);
			if(!actName.equals("开始")&&!actName.equals("结束")){
				arr.put(jsonObject1);
			}
		}
		return arr;
	}

	@Override
	public String delEvectionList(String id,String eveId) {
		String responseText = FAIL;
		try {
			EmEvectionManage emEvectionManage = this.lookupService(EmEvectionManage.class);
		    DataParam param = new DataParam();
		    param.put("EXP_ID",id);
		    emEvectionManage.deleteExpRecord(param);
			emEvectionManage.computeTotalMoney(eveId);
		    responseText = SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return responseText;
	}



}
